﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using Billing.Win;
using System.Linq;

namespace Billing.Transactions
{
    public partial class MakePaymentFromOrderForm : Billing.Transactions.TransactionBase
    {
        public MakePaymentFromOrderForm()
        {
            InitializeComponent();
            this.AddNewHeader();
            this.dtDocumentDate.MaxDate = DateTime.Now;
            this.dtDocumentDate.MinDate = this.BillingDataContext.GetClosedPeriod().Value.AddDays(1);
            this.CurrentHeader.HeaderTypeID = 8;
        }

        PaymentOrder PO = null;
        private void trvPaymentOrder_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            PaymentOrdersForm frm = new PaymentOrdersForm(false);
            frm.AllowSelect = true;
            frm.AllowAdd = false;
            frm.AllowDelete = false;
            frm.AllowEdit = false;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                PO = (PaymentOrder)frm.SelectedObjects[0].CurrentObject;
                this.trvPaymentOrder.BaseObject = PO;
                this.txtAmount.Value = PO.Amount;
                this.dtPaymentOrderDate.Value = PO.DocumentDate;
                this.dtDocumentDate.Value = DateTime.Today;

                this.CurrentHeader.AccountingDate = PO.DocumentDate;
                this.CurrentHeader.PaymentPointID = PO.PaymentPointID;
                
            }
        }

        private void grdAccounts_ClickCellButton(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
        {
            if (e.Cell.Column.Key.ToLower() == "account")
            {
                AccountsSelectionForm frm = new AccountsSelectionForm(false, true);
                // ClientsSelectionForm  frm = new ClientsSelectionForm();
                frm.AllowSelect = true;
                AccountsView av = null;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    av = (AccountsView)frm.SelectedObjects[0].CurrentObject;
                    e.Cell.Value = (from i in this.BillingDataContext.Accounts
                                    where i.ID == (long)av.Value
                                    select i).First();
                    e.Cell.Row.Cells["AccountType"].Value = av.AccountType.ToString();
                    e.Cell.Row.Cells["ClientName"].Value = av.Name;
                    e.Cell.Row.Cells["Amount"].Value = this.txtAmount.Value;
                    e.Cell.Row.Cells["ModuleID"].Value = (short)2;
                    e.Cell.Row.Cells["ModuleObjectID"].Value = PO.ID;
                    this.trvPaymentOrder.Enabled = false;
                    this.dtDocumentDate.ReadOnly = true;
                }
            }
        }
        public override void OnSave()
        {
            try
            {
                this.CurrentHeader.DocumentDate = (DateTime) this.dtDocumentDate.Value;
                this.bsHeader.EndEdit();
                this.bsLines.EndEdit();

                if (PO == null)
                    throw new Exception("გადახდის ორდერი არ არის არჩეული.");
                if ((decimal)this.grdAccounts.Rows.SummaryValues["SumAmount"].Value != (decimal)this.txtAmount.Value)
                    throw new Exception("გადახდის ორდერის და გატარებების თანხა არ არის ტოლი.");
                this.BillingDataContext.Connection.Open();
                this.BillingDataContext.Transaction = this.BillingDataContext.Connection.BeginTransaction();
                this.BillingDataContext.PaymentOrderCommit(PO.ID);
                this.BillingDataContext.SubmitChanges();
                this.BillingDataContext.Transaction.Commit();
                ShowInformation("გატარებები დასრულებულია.");
                this.Close();
            }
            catch (Exception ex)
            {
                if (this.BillingDataContext.Transaction != null)
                {
                    this.BillingDataContext.Transaction.Rollback();
                    this.BillingDataContext.Connection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }
    }
}
